package manage.actions.dept;

import java.io.InputStream;

import manage.actions.ManageAction;
import manage.commons.annontations.AccessControl;
import manage.commons.annontations.AccessVerify;
import manage.commons.annontations.RequiredLogin;
import manage.commons.utils.InputStreamUtil;
import manage.commons.utils.Pager;
import manage.modules.dept.model.Dept;
import manage.modules.dept.service.DeptService;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

@RequiredLogin
@AccessVerify
public class DeptAction extends ManageAction {

	private static final long serialVersionUID = 4249886187419780524L;

	private DeptService deptService;
	private Pager<Dept> pager = new Pager<Dept>(100);
	private Dept dept;

	@Override
	@Action("dept-input")
	@AccessControl
	public String input() {
		return SUCCESS;
	}

	@Override
	@AccessVerify(-1)
	@Action(value = "dept-addorupdate", results = { @Result(name = SUCCESS, location = "/dept/dept-form.jsp") })
	public String forwardAddOrUpdatePage() {
		if (isAddOperater()) {
			action = "/dept/dept-add.do";
			getRequest().setAttribute("btnKeyWord", "添加部门");
		} else if (isUpdateOperater()) {
			action = "/dept/dept-edit.do";
			getRequest().setAttribute("btnKeyWord", "更新部门");
			dept = deptService.select(dept.getDeptId());
		}
		return SUCCESS;
	}

	@AccessControl
	@Action(value = "dept-query", results = { @Result(name = SUCCESS, location = "/dept/dept-list.jsp") })
	public String query() throws Exception {
		deptService.query(dept, pager);
		pager.pagerUtils(pager, getRequest());
		return SUCCESS;
	}

	@AccessVerify(-1)
	@Action(value = "dept-select", results = { @Result(name = SUCCESS, type = "json", params = {
			"root", "dept" }) })
	public String select() {
		if (null != dept && null != dept.getDeptId()) {
			boolean exists = deptService.exists(dept.getDeptId());
			if (exists) {
				dept = deptService.select(dept.getDeptId());
			}
		}
		return SUCCESS;
	}

	@Action(value = "dept-add", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String add() throws Exception {
		if (null != dept && null != dept.getDeptId()
				&& dept.getDeptName() != null && dept.getAbility() != null) {
			boolean exists = deptService.exists(dept.getDeptId());
			if (!exists) {
				int count = deptService.insert(dept);
				if (count == 1) {
					inputStream = InputStreamUtil.getSuccessInputStream();
				} else {
					inputStream = InputStreamUtil.getFailedInputStream();
				}
			} else {
				inputStream = InputStreamUtil.getExistsInputStream();
			}
		}
		return SUCCESS;
	}

	@Action(value = "dept-delete", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String delete() throws Exception {
		if (null != dept && null != dept.getDeptId()) {
			boolean exists = deptService.exists(dept.getDeptId());
			if (exists) {
				if (!deptService.groupVested(dept.getDeptId())) {
					int delNum = deptService.delete(dept.getDeptId());
					if (delNum == 1) {
						inputStream = InputStreamUtil.getSuccessInputStream();
					}
				} else {
					inputStream = InputStreamUtil.toInputStream("deptvested");
				}
			} else {
				inputStream = InputStreamUtil.getNotExistsInputStream();
			}
		} else {
			inputStream = InputStreamUtil.getFailedInputStream();
		}
		return SUCCESS;
	}

	@Action(value = "dept-edit", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String edit() throws Exception {
		if (null != dept && null != dept.getDeptId()
				&& dept.getDeptName() != null && dept.getAbility() != null) {
			boolean exists = deptService.exists(dept.getDeptId());
			if (exists) {
				int count = deptService.update(dept);
				if (count >= 1) {
					inputStream = InputStreamUtil.getSuccessInputStream();
				} else {
					inputStream = InputStreamUtil.getNoChangeInputStream();
				}
			} else {
				inputStream = InputStreamUtil.getFailedInputStream();
			}
		}
		return SUCCESS;
	}

	public Pager<Dept> getPager() {
		return pager;
	}

	public void setPager(Pager<Dept> pager) {
		this.pager = pager;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public Dept getDept() {
		return dept;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}

}
